import moment from "moment";

interface Address {
	addrProvince: string;
	addrCity: string;
	addrDistrict: string;
	addrCommune: string;
	addrWard: string;
	addrHouseno: string;
}

export function getAddress(obj: Address & Recordable): string {
	let sorts = ["addrProvince", "addrCity", "addrDistrict", "addrCommune", "addrWard", "addrHouseno"];
	let str = "";
	sorts.forEach(key => {
		str += Reflect.has(obj, key) && obj[key] ? obj[key] : "";
	});
	return str;
}

export function getAge(birthday: string | Date): number | null {
	if (!birthday) return null;
	const today = moment();
	const birthDate = moment(birthday);
	let age = today.diff(birthDate, "years");
	// 如果已经过了今年的生日，则年龄加1
	if (today.isAfter(birthDate.clone().add(1, "year"))) age += 1;
	return age;
}
